package com.mk.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mk.context.BaseContext;
import com.mk.entity.Trouble;
import com.mk.result.Result;
import com.mk.service.ITroubleRemovalService;
import com.mk.vo.TroubleVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 事故处理业务
 */
@RestController
@RequestMapping("/user/trouble")
@RequiredArgsConstructor
public class TroubleRemovalController {

    private final ITroubleRemovalService troubleRemovalService;

    /**
     * 事故快处
     * @param trouble
     * @return
     */
    @CrossOrigin
    @PostMapping("/submit")
    public Result submit(@RequestBody Trouble trouble) {
        trouble.setWxId(BaseContext.getCurrentId());
        trouble.setCreateTime(LocalDateTime.now());
        trouble.setUpdateTime(LocalDateTime.now());
        troubleRemovalService.save(trouble);
        return Result.success();
    }

    /**
     * 事故取消
     * @param id
     * @return
     */
    @CrossOrigin
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        troubleRemovalService.update(new LambdaUpdateWrapper<Trouble>()
                .set(Trouble::getStatus, Trouble.CANCEL)
                .eq(Trouble::getId, id));
        return Result.success();
    }

    /**
     * 根据id查询事故处理进度
     * @param id
     * @return
     */
    @CrossOrigin
    @GetMapping("/schedule/{id}")
    public Result<TroubleVO> getSchedule(@PathVariable Long id) {
        Trouble trouble = troubleRemovalService.getById(id);
        return Result.success(BeanUtil.copyProperties(trouble, TroubleVO.class));
    }

    /**
     * 根据id事故证明查询（照片）
     * @param id
     * @return
     */
    @CrossOrigin
    @GetMapping("/certify/{id}")
    public Result<TroubleVO> getQuery(@PathVariable Long id) {
        Trouble trouble = troubleRemovalService.getOne(new LambdaQueryWrapper<Trouble>()
                .select(Trouble::getCertify)
                .eq(Trouble::getId, id));
        return Result.success(BeanUtil.copyProperties(trouble, TroubleVO.class));
    }
}
